package com.itheima.protocol.parent;

import com.itheima.protocol.serializable.Person;

import java.io.*;


/**
 * 序列化不保存静态变量
 * 被transient关键字修饰的属性，不参与序列化
 */
public class SerializableUserDemo {

    public static void main(String[] args) {

        //序列化
        serializeUser();

        //反序列化
        User user = DeserializeUser();
        System.out.println(user);
    }

    private static void serializeUser(){

        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("user")));

            User user = new User();
            user.setAge(29);
            outputStream.writeObject(user);

            System.out.println("序列化完成");

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static User DeserializeUser() {

        try {
            ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(new File("user")));

            User user = (User)inputStream.readObject();
            System.out.println("反序列化成功");
            System.out.println(user.toString());

            return user;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
